// pages/print/idCard/idCard.js
const utils = require("../../../utils/utils");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        code: '',
        chooseImg: '',
        zmimg: '../../../img/45.png',
        fmimg: '../../../img/46.png',
        chooseIndex: 0,
        zmOfm: 'zm',
        robotzm: 0, //0 自动  1手动
        robotfm: 0,
        zmOss: '',
        fmOss: ''
    },
    changeImg(e) {
        var index = e.currentTarget.dataset.index;
        this.setData({
            chooseIndex: index
        })
    },
    btnConfirm() {
        wx.navigateTo({
            url: '../pay/pay',
        })
    },
    goUrl(e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        wx.showActionSheet({
            itemList: ['相册', '拍照', '从QQ会话选择'],
            success(res) {
                if (res.tapIndex == 0) {
                    that.getPhoto('album', index);
                }
                if (res.tapIndex == 1) {
                    that.getPhoto('camera', index);
                }
                if (res.tapIndex == 2) {
                    that.getPhoto1(index);
                }
            },
            fail(res) {

            }
        })
    },
    //拍照调取相册
    getPhoto(type, index) {
        var that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['original'],
            sourceType: [type],
            success(res) {
                const tempFilePaths = res.tempFilePaths[0];
                if (index == 'zm') {
                    that.setData({
                        chooseImg: tempFilePaths,
                        zmOfm: 'zm'
                    })
                    that.delImg();
                    // wx.navigateTo({
                    //   url: '../photoEdit/photoEdit?code='+code+'&w=305.71428571428&h=192.85714285714&type=zm&img='+tempFilePaths,
                    // })
                } else {
                    that.setData({
                        chooseImg: tempFilePaths,
                        zmOfm: 'fm'
                    })
                    that.delImg();
                    // wx.navigateTo({
                    //   url: '../photoEdit/photoEdit?code='+code+'&w=305.71428571428&h=192.85714285714&type=fm&img='+tempFilePaths,
                    // })
                }
            }
        })
    },
    //图片先进行处理
    delImg() {
        // wx.showLoading({
        //     title: '处理中',
        // })
       
        var that = this;
        that.selectComponent("#loading").showLoadingCus('处理中...');
        var base64 = that.data.chooseImg;
        utils.requestId(base64, function (res) {
            var data = JSON.parse(res.data).data;
            if (data.cut == 1) {
                if (that.data.zmOfm == 'zm') {
                    that.setData({
                        zmimg: data.fileUrl
                    })
                    that.selectComponent("#loading").hideLoadingCus();
                }
                if (that.data.zmOfm == 'fm') {
                    that.setData({
                        fmimg: data.fileUrl
                    })
                    that.selectComponent("#loading").hideLoadingCus();
                }
            } else {
                that.selectComponent("#loading").hideLoadingCus();
                wx.showModal({
                    title: '温馨提示',
                    content: '自动裁剪失败，点击确认进行手动裁剪',
                    success(res) {
                        if (res.confirm) {
                            wx.navigateTo({
                                url: '../photoEdit/photoEdit?code=' + that.data.code + '&w=305.71428571428&h=192.85714285714&type=' + that.data.zmOfm + '&img=' + base64,
                            })
                        } else if (res.cancel) {
                            that.setData({
                                zmOfm: '',
                                chooseImg: ''
                            })
                        }
                    }
                })
            }
            that.selectComponent("#loading").hideLoadingCus();
        })
    },
    getPhoto1(index) {
        var that = this;
        wx.chooseMessageFile({
            count: 1,
            type: 'image',
            success(res) {
                that.selectComponent("#loading").showLoadingCus('');
                if (index == 'zm') {
                    that.setData({
                        chooseImg: res.tempFiles[0].path,
                        zmOfm: 'zm'
                    })
                    that.delImg();
                    // wx.navigateTo({
                    //   url: '../photoEdit/photoEdit?code='+code+'&w=305.71428571428&h=192.85714285714&type=zm&img='+res.tempFiles[0].path,
                    // })
                } else {
                    that.setData({
                        chooseImg: res.tempFiles[0].path,
                        zmOfm: 'fm'
                    })
                    that.delImg();
                    // wx.navigateTo({
                    //   url: '../photoEdit/photoEdit?code='+code+'&w=305.71428571428&h=192.85714285714&type=fm&img='+res.tempFiles[0].path,
                    // })
                }
            }
        })
    },
    //上传正面
    uploadZm() {
        //先判断是否需要上传正面
        var that = this;
        var zmimg = that.data.zmimg;
        var fmimg = that.data.fmimg;
        if (zmimg == '../../../img/45.png') {
            utils.toast('请上传身份证正面')
            return false;
        }
        if (fmimg == '../../../img/46.png') {
            utils.toast('请上传身份证反面')
            return false;
        }
        that.selectComponent("#loading").showLoadingCus('');
        that.compoundPhoto();
    },
    //上传反面
    uploadFm() {
        //先判断是否需要上传反面
        var that = this;
        var fmimg = that.data.fmimg;
        if (fmimg == '../../../img/46.png') {
            that.selectComponent("#loading").hideLoadingCus();
            utils.toast('请上传身份证反面')
            return false;
        }
        if (that.data.robotfm == 1) {
            that.compoundPhoto();
        } else {
            //   var img = fmimg.replace('data:image/png;base64,','')
            utils.request('Index/fileAddBase64', {
                baseData: fmimg,
            }, function (res) {
                if (res.data.code == 1) {
                    that.setData({
                        fmOss: res.data.url
                    })
                    that.compoundPhoto();
                } else {
                    utils.toast(res.data.msg)
                    that.selectComponent("#loading").hideLoadingCus();
                }
            })
        }
    },
    //身份证合成
    compoundPhoto() {
        var that = this;
        var zmimg = that.data.zmimg;
        var fmimg = that.data.fmimg;
        var data = {
            frontUrl: zmimg,
            backUrl: fmimg,
            sign: that.data.chooseIndex==1?true:false
        }
        utils.request('convert/idCardMerge', data, function (res) {
            that.selectComponent("#loading").hideLoadingCus();
            if (res.data.code == 200) {
                var code = that.data.code;
                wx.redirectTo({
                    url: '../list/list?code=' + code + '&name=身份证&pageNum=1&path=' + res.data.data.fileUrl+'&fileUrl='+res.data.data.fileUrl+'&serviceMainId='+that.data.serviceMainId+'&serviceSubtypeId=&tagFloor='+that.data.code+'&orientation=1'
                })
            } else {
                utils.toast(res.data.msg)
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            code: options.code,
            serviceMainId:options.serviceMainId
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
})